package org.veridiandynamics.server.model;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.veridiandynamics.server.dao.HibernateDriver;
import org.veridiandynamics.server.dao.Queryprocessor;
import org.veridiandynamics.server.domain.Patient;
import org.veridiandynamics.server.domain.TreatmentRecord;

public class PatientModel {
	
	private HibernateDriver driver = new HibernateDriver();

	public List<Integer> getWeightHistory(String username) {
		Long id = Queryprocessor.UsernametoID(username);
		Patient patient = (Patient) driver.RetrieveObject(id, new Patient());
		List<Integer> weightHistory = new ArrayList<Integer>();
		List<Date> dateHistory = new ArrayList<Date>();
		Iterator<TreatmentRecord> iterator = patient.getMyTreatmentRecordList().iterator();
		while(iterator.hasNext()) {
			TreatmentRecord record = iterator.next();
			weightHistory.add(record.getWeight());
			dateHistory.add(record.getTreatmentDate());
		}
		
		for(int i=1; i<dateHistory.size(); i++){
			int j=i;
			Date B = dateHistory.get(i);
			Integer C = weightHistory.get(i);
			while((j>0)&&(dateHistory.get(j-1).compareTo(B)>0)){
				Date dateReplacement = dateHistory.get(j-1);
				Integer weightReplacement = weightHistory.get(j-1);
				dateHistory.set(j, dateReplacement);
				weightHistory.set(j, weightReplacement);
			}
			dateHistory.set(j, B);
			weightHistory.set(j,C);
		}
		return weightHistory;
	}

	
}
